package top.wshape1.easyscan.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;

import androidx.annotation.IdRes;

import java.util.List;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.entity.CustomRule;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-05-21
 */
public class SettingData {

    public final String SHARED_PREFERENCES_NAME = "Setting";
    private final EasyScanActivity mainActivity;

    private final SharedPreferences sharedPreferences;

    private final SettingSQLiteHelper settingSQLiteHelper;
    private final SharedPreferences.Editor editor;

    private final String DEFAULT_FRAGMENT = "default_fragment";
    private final String URL_JUMP = "url_jump";

    public SettingData(EasyScanActivity mainActivity) {
        this.mainActivity = mainActivity;
        sharedPreferences = mainActivity.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        settingSQLiteHelper = new SettingSQLiteHelper(mainActivity);
    }

    public void setDefaultFragment(@IdRes int itemId) {
        editor.putInt(DEFAULT_FRAGMENT, itemId);
        editor.apply();
    }

    public int getDefaultFragment() {
        return sharedPreferences.getInt(DEFAULT_FRAGMENT, R.id.navigation_home);
    }

    public void setUrlJump(Boolean isJump) {
        editor.putBoolean(URL_JUMP, isJump);
        editor.apply();
    }

    public Boolean isUrlJump() {
        return sharedPreferences.getBoolean(URL_JUMP, true);
    }

    public int getCustomRuleCount() {
        return settingSQLiteHelper.count();
    }

    public List<CustomRule> getCustomRuleList() {
        return settingSQLiteHelper.list();
    }

    public int removeCustomRule(int id) {
        return settingSQLiteHelper.remove(id);
    }

    public long addCustomRule(CustomRule customRule) {
        return settingSQLiteHelper.insert(customRule);
    }

    public int updateCustomRule(CustomRule customRule) {
        return settingSQLiteHelper.update(customRule);
    }

}
